package csc.fresher.view;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.dao.DepositAccountDao;
import csc.fresher.entity.CustomerAccount;
import csc.fresher.entity.CustomerAccountOverall;
import csc.fresher.entity.DepositAccount;
import csc.fresher.entity.SystemUser;
import csc.fresher.service.DepositAccountService;
import csc.fresher.service.SystemUserService;

@Component
@Scope("session")
public class SavingManageBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private DepositAccountService depositManageService;

	@Autowired
	private SystemLoginManageBean systemLoginManageBean;

	@Autowired
	private SystemUserService systemUserService;

	
	private static Pattern pattern;
	private static Matcher matcher;
	private final String idCardNumber_pattern = "^\\s*\\d+\\s*$";
	private String selectSearchType;
	private List<CustomerAccountOverall> savingAccountList;
	private String searchValue;
	private CustomerOverallDataModel savingAccountListDataModel;
	private CustomerAccountOverall customerAccountOverall;

	public void onLoad() {
		SystemUser systemUser;

		if (systemLoginManageBean.getSystemUser() == null) {
			try {
				FacesContext.getCurrentInstance().getExternalContext()
						.redirect("/csc_final/templates3/Logon.jsf");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			systemUser = systemLoginManageBean.getSystemUser();
			List<Long> listrole = systemUserService
					.getRoleByUserName(systemUser.getUserName());
			boolean result = checkRole(listrole);
			if (result == false) {
				try {
					FacesContext.getCurrentInstance().getExternalContext()
							.redirect("AccountManagement.xhtml");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public boolean checkRole(List<Long> listRole) {

		for (int i = 0; i < listRole.size(); i++) {
			if (3 == listRole.get(i) || 4 == listRole.get(i)) {
				return true;
			}
		}
		return false;
	}

	@PostConstruct
	public void init() {
		this.customerAccountOverall = null;
		this.savingAccountList = firstLoad();
		this.savingAccountListDataModel = new CustomerOverallDataModel(
				this.savingAccountList);

	}

	// first load
	public List<CustomerAccountOverall> firstLoad() {
		savingAccountList = depositManageService.getAccountByState("new",
				"SavingAccount");
		return savingAccountList;
	}

	// search button
	public String search() {
		if(checkSearchValue(searchValue)){		
		
		if ("idCardNumber".equals(selectSearchType)) {
			try{
				if(checkIdCardNumber(searchValue)){
					long idCardNumber = Long.parseLong(searchValue);
					savingAccountListDataModel = new CustomerOverallDataModel(
							depositManageService.getAccountByIDCardNumber(
									idCardNumber, "SavingAccount"));
				}
			
			}catch(Exception e){
				e.printStackTrace();
			}
		
		} else if ("fristName".equals(selectSearchType)) {
			savingAccountListDataModel = new CustomerOverallDataModel(
					depositManageService.getAccountByFirstName(searchValue,
							"SavingAccount"));
		} else if ("state".equals(selectSearchType)) {
			savingAccountListDataModel = new CustomerOverallDataModel(
					depositManageService.getAccountByState(searchValue,
							"SavingAccount"));
		} else if ("phoneNumber".equals(selectSearchType)) {
			savingAccountListDataModel = new CustomerOverallDataModel(
					depositManageService.getAccoungByPhoneNumber(searchValue,
							"SavingAccount"));
		} else if ("address".equals(selectSearchType)) {
			savingAccountListDataModel = new CustomerOverallDataModel(
					depositManageService.getAccountByAddress(searchValue,
							"SavingAccount"));
		}}
		return "savingAccountManagement";
	}

	// edit button
	public String edit() {
		customerAccountOverall = null;
		customerAccountOverall = savingAccountListDataModel.getRowData();
		return "UpdateSavingAccount";
	}

	public String getSelectSearchType() {
		return selectSearchType;
	}

	public void setSelectSearchType(String selectSearchType) {
		this.selectSearchType = selectSearchType;
	}

	public List<CustomerAccountOverall> getDepositAccountList() {
		return savingAccountList;
	}

	public void setDepositAccountList(
			List<CustomerAccountOverall> depositAccountList) {
		this.savingAccountList = depositAccountList;
	}

	public String getSearchValue() {
		return searchValue;
	}

	public void setSearchValue(String searchValue) {
		this.searchValue = searchValue;
	}

	public DataModel<CustomerAccountOverall> getSavingAccountListDataModel() {
		return savingAccountListDataModel;
	}

	public void setSavingAccountListDataModel(
			CustomerOverallDataModel depositAccountListDataModel) {
		this.savingAccountListDataModel = depositAccountListDataModel;
	}

	public CustomerAccountOverall getCustomerAccountOverall() {
		return customerAccountOverall;
	}

	public void setCustomerAccountOverall(CustomerAccountOverall customerAccount) {
		this.customerAccountOverall = customerAccount;
	}
	
	public boolean checkSearchValue(String searchValue){
		boolean result = false;
		try {
		if(null == searchValue || searchValue == "" || "".equals(searchValue) || searchValue.isEmpty()){
			result = false;
		}else{
			result = true;
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	public boolean checkIdCardNumber(String idCardNumber) {

		pattern = Pattern.compile(idCardNumber_pattern);
		matcher = pattern.matcher(idCardNumber);
		return matcher.matches();

	}
}
